* 4_Scatterplots
** Goal: Generate main regression tables 
** Beatrice Lee
** Date Created: 2023-01-31 (split from the mainsampleresults file on 2024-07-09)
** Last Updated: 2024-07-31 // tja

*** load dataset ***
cd "$Data/Processing"
use state_elections_panel_df5_20240731, clear

**************************************** FIGURES **************************************************

* Scatterplots  -------------------------------

* Norm vote vs Inc vote share - main sample
cd "$Charts"
scatter incumbent_vote_share_top2 norm_vote_top2_share_sd if year>= 2020, msize(tiny) mlabel(state) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small)) legend(off) xtitle("Normal Vote - Incumbent party's share of top 2 candidate's total votes" "from latest pre-pandemic election for all but edge cases") ytitle("Incumbent party's share of top 2 candidate's total votes" "(current election)") || lfit incumbent_vote_share_top2 norm_vote_top2_share_sd if year >= 2020

cd "$Charts"
graph export "incmargin_norm_vote_top2_share_sd_20240731.png", as("png") width (6000) replace

* Norm vote vs Inc vote share - placebo sample
cd "$Charts"
scatter incumbent_vote_share_top2 norm_vote_top2_share_sd if (year >= 2013 & year <= 2019), msize(tiny) mlabel(state) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small)) legend(off) xtitle("Normal Vote - Incumbent party's share of top 2 candidate's total votes" "from latest pre-pandemic election for all but edge cases") ytitle("Incumbent party's share of top 2 candidate's total votes" "(current election)") || lfit incumbent_vote_share_top2 norm_vote_top2_share_sd if (year >= 2013 & year <= 2019)

cd "$Charts"
graph export "placebo_sample_incmargin_norm_vote_top2_share_sd_20240731.png", as("png") width (6000) replace

* Reps per mill vs Inc vote share - main sample
cd "$Charts"
scatter incumbent_vote_share_top2 reps_per_million if year>= 2020, msize(tiny) mlabel(state) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small)) legend(off) xtitle("Representatives per million" ) ytitle("Incumbent party's share of top 2 candidate's total votes" "(current election)") || lfit incumbent_vote_share_top2 reps_per_million if year >= 2020

cd "$Charts"
graph export "incmargin_norm_reps_per_mill_20240731.png", as("png") width (6000) replace

* Reps per mill vs Inc vote share - placebo sample
cd "$Charts"
scatter incumbent_vote_share_top2 reps_per_million if (year >= 2013 & year <= 2019), msize(tiny) mlabel(state) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small)) legend(off) xtitle("Representatives per million" ) ytitle("Incumbent party's share of top 2 candidate's total votes" "(current election)") || lfit incumbent_vote_share_top2 reps_per_million if (year >= 2013 & year <= 2019)

cd "$Charts"
graph export "placebo_sample_incmargin_norm_reps_per_mill_20240731.png", as("png") width (6000) replace


* Residualized Scatterplots: Main Sample  -------------------------------
reg reps_per_million norm_vote_top2_share_sd if year >= 2020, vce(cluster state)
predict fitted_reps_excl
gen reps_residual_excl = reps_per_million-fitted_reps_excl

reg incumbent_vote_share_top2 norm_vote_top2_share_sd if year >= 2020, vce(cluster state)
predict fitted_inc_vote_share_top2_excl
gen inc_vote_share_top2_excl = incumbent_vote_share_top2 - fitted_inc_vote_share_top2_excl

reg inc_vote_share_top2_excl reps_residual_excl if year >= 2020, vce(cluster state)

local slope = round((_b[reps_residual_excl]),0.001)
local slope : di %3.2f `slope'
local intercept = round((_b[_cons]),0.001)
local intercept : di %3.2f `intercept'
local sterr = round((_se[reps_residual_excl]),0.001)
local sterr : di %3.2f `sterr'

* Residualized Incumbent Vote Share vs Congressional Representation
twoway lfitci inc_vote_share_top2_excl reps_residual_excl if year >= 2020, legend(off) xlabel(-2(1)3) || scatter inc_vote_share_top2_excl reps_residual_excl if year >= 2020, mlabel(state) xlabel(-2(1)3) msymbol(circle) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small))  ylabel(-30(10)30) xtitle("Residualized - Number of representatives per million residents") ytitle("Residualized - Incumbent Party Vote Share")

cd "$Charts"
graph export "resid_scatter_reps_ivc_vs_plain_20240731.png", as("png") replace

* BV scatterplot: Main Sample ------------------------------------------
twoway lfitci incumbent_vote_share_top2 reps_per_million if year >= 2020, legend(off) xlabel(1(1)5) || scatter incumbent_vote_share_top2 reps_per_million if year >= 2020, mlabel(state) xlabel(1(1)5) msymbol(circle) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small))  ylabel(30(10)80) xtitle("Number of representatives per million residents") ytitle("Incumbent Party Vote Share")

* Export Regressions for comparison
cd "$Charts"
graph export "scatter_reps_ivc_vs_plain.png", as("png") replace

cd "$results"
global output"$Results/residualized_comp_20240731.xls"

reg incumbent_vote_share_top2 reps_per_million if year >= 2020, vce(cluster state)
outreg2 using "$output", append ctitle(BV)

reg inc_vote_share_top2_excl reps_residual_excl if year >= 2020, vce(cluster state)
outreg2 using "$output", append ctitle(Residualized)

* drop variables to run the placebo sample
drop fitted_reps_excl
drop reps_residual_excl
drop fitted_inc_vote_share_top2_excl
drop inc_vote_share_top2_excl

* Residualized Scatterplots: Placebo Sample  -------------------------------
reg reps_per_million norm_vote_top2_share_sd if (year >= 2013 & year <= 2019), vce(cluster state)
predict fitted_reps_excl
gen reps_residual_excl = reps_per_million-fitted_reps_excl

reg incumbent_vote_share_top2 norm_vote_top2_share_sd if (year >= 2013 & year <= 2019), vce(cluster state)
predict fitted_inc_vote_share_top2_excl
gen inc_vote_share_top2_excl = incumbent_vote_share_top2 - fitted_inc_vote_share_top2_excl

reg inc_vote_share_top2_excl reps_residual_excl if (year >= 2013 & year <= 2019), vce(cluster state)

local slope = round((_b[reps_residual_excl]),0.001)
local slope : di %3.2f `slope'
local intercept = round((_b[_cons]),0.001)
local intercept : di %3.2f `intercept'
local sterr = round((_se[reps_residual_excl]),0.001)
local sterr : di %3.2f `sterr'

* Residualized Incumbent Vote Share vs Congressional Representation
twoway lfitci inc_vote_share_top2_excl reps_residual_excl if (year >= 2013 & year <= 2019), legend(off) xlabel(-2(1)3) || scatter inc_vote_share_top2_excl reps_residual_excl if (year >= 2013 & year <= 2019), mlabel(state) xlabel(-2(1)3) msymbol(circle) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small))  ylabel(-50(10)40) xtitle("Residualized - Number of representatives per million residents") ytitle("Residualized - Incumbent Party Vote Share")

cd "$Charts"
graph export "placebo_resid_scatter_reps_ivc_vs_plain_20240731.png", as("png") replace

* BV scatterplot: Placebo Sample ------------------------------------------
twoway lfitci incumbent_vote_share_top2 reps_per_million if (year >= 2013 & year <= 2019), legend(off) xlabel(1(1)5) || scatter incumbent_vote_share_top2 reps_per_million if (year >= 2013 & year <= 2019), mlabel(state) xlabel(1(1)5) msymbol(circle) mcolor(blue) mlabcolor(black) graphregion(fcolor(white)) ytitle(,size(small))  ylabel(0(10)100) xtitle("Number of representatives per million residents") ytitle("Incumbent Party Vote Share")

cd "$Charts"
graph export "placebo_scatter_reps_ivc_vs_plain_20240731.png", as("png") replace

* Export Regressions for comparison

cd "$results"
global output"$Results/placebo_residualized_comp_20240731.xls"

reg incumbent_vote_share_top2 reps_per_million if (year >= 2013 & year <= 2019), vce(cluster state)
outreg2 using "$output", append ctitle(BV)

reg inc_vote_share_top2_excl reps_residual_excl if (year >= 2013 & year <= 2019), vce(cluster state)
outreg2 using "$output", append ctitle(Residualized)
